package com.axend.aerosense.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.axend.aerosense.base.utils.LogUtils;


/**
 * 热力图模块，采用WebView实现，加载assets文件夹的heatmap.html
 * @author ChenHongWei
 * @date :2020/12/7 15:28
 */

public class Heatmap extends WebView {

    private String loadUrl = "file:///android_asset/heatmap/heatmap.html";

    public Heatmap(Context context) {
        super(context);
    }

    public Heatmap(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Heatmap(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        WebSettings webSettings = getSettings();
        //设置启动Js
        webSettings.setJavaScriptEnabled(true);
        //设置JavaScript可以自动打开Windows
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //设置支持缩放
        webSettings.setSupportZoom(false);
        //设置显示控件缩放
        webSettings.setDisplayZoomControls(false);
        //加载Html的文件地址
        loadUrl(loadUrl);
        //添加回调
        addJavascriptInterface(new JsCallAndroid(), "androidObj");
    }

    //设置加载的url
    public void setLoadUrl(String loadUrl) {
        this.loadUrl = loadUrl;
    }



    //js调用Android代码
    public static class JsCallAndroid{

        @JavascriptInterface
        public void jsCallback(String msg){
            LogUtils.d(this, msg);
        }
    }
}
